package com.narbeshuber.tablet.lasalara.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.narbeshuber.tablet.lasalara.LasaLaraActivity;
import com.narbeshuber.tablet.lasalara.R;
import com.narbeshuber.tablet.lasalara.Utils;
import com.narbeshuber.tablet.lasalara.http.LoginTask;
import com.narbeshuber.tablet.lasalara.http.RetrievePasswordTask;
import com.narbeshuber.tablet.lasalara.http.SignUpTask;

public class LoginFragment extends Fragment {
	
	private static TextView loginEmailError;
	private static TextView loginPasswordError;
	private static TextView signupEmailError;
	private static TextView signupReEmailError;
	private static TextView signupPasswordError;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.loginlayout, container, false);
		
		loginEmailError = (TextView) v.findViewById(R.id.login_email_error);
		loginPasswordError = (TextView) v.findViewById(R.id.login_password_error);
		signupEmailError = (TextView) v.findViewById(R.id.signup_email_error);
		signupReEmailError = (TextView) v.findViewById(R.id.signup_re_email_error);
		signupPasswordError = (TextView) v.findViewById(R.id.signup_password_error);
		
		Button loginButton = (Button)v.findViewById(
				R.id.login_button);

		loginButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				clearErrorFields();
				EditText emailField = (EditText)getActivity().findViewById(R.id.email);
				EditText passwordField = (EditText)getActivity().findViewById(R.id.password);
				if(!(emailField.getText().toString().equals("")) && !(passwordField.getText().toString().equals(""))){
					if(Utils.isOnline((LasaLaraActivity)getActivity())){
						new LoginTask((LasaLaraActivity)getActivity()).execute(emailField.getText().toString().trim(), 
								passwordField.getText().toString().trim());
					}
				}else{		
					if(emailField.getText().toString().equals("")){
						setErrorText("Field is empty", ErrorField.LOGIN_EMAIL);
					}
					if(passwordField.getText().toString().equals("")){
						setErrorText("Field is empty", ErrorField.LOGIN_PASSWORD);
					}
				}
			}
		});
		
		Button passwordLostButton = (Button)v.findViewById(
				R.id.password_lost_button);
		passwordLostButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				clearErrorFields();
				EditText emailField = (EditText)getActivity().findViewById(R.id.email);
				if(!(emailField.getText().toString().equals(""))){
					if(Utils.isOnline((LasaLaraActivity)getActivity())){
						new RetrievePasswordTask((LasaLaraActivity)getActivity()).execute(emailField.getText().toString().trim());
					}
				}else{
					setErrorText("Field is empty", ErrorField.LOGIN_EMAIL);
				}
				
			}
		});
		
		Button signupButton = (Button)v.findViewById(
				R.id.signup_button);
		signupButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				clearErrorFields();

				String email = ((EditText) getActivity().findViewById(R.id.signup_email)).getText().toString().trim();
				String re_email = ((EditText) getActivity().findViewById(R.id.signup_re_email)).getText().toString().trim();
				String password = ((EditText) getActivity().findViewById(R.id.signup_password)).getText().toString().trim();

				if(email.equals(re_email) && !email.equals("") && !password.equals("") && password.length() > 3 && Utils.emailValidation(email)){
					if(Utils.isOnline((LasaLaraActivity)getActivity())){
						new SignUpTask((LasaLaraActivity)getActivity()).execute(email.trim(), 
								password.trim());
					}
				}else{
					if (!email.equals(re_email) && Utils.emailValidation(email)) {
						setErrorText("E-mails don't match",
								ErrorField.SIGNUP_EMAIL);
						setErrorText("E-mails don't match",
								ErrorField.SIGNUP_RE_EMAIL);
						if (password.equals("")) {
							setErrorText("Field is empty",
									ErrorField.SIGNUP_PASSWORD);
						} else if (!(password.length() > 3)) {
							setErrorText(
									"Min 4 characters",
									ErrorField.SIGNUP_PASSWORD);
						}
					} else {

						if (email.equals("")) {
							setErrorText("Field is empty",
									ErrorField.SIGNUP_EMAIL);
						} else if (!Utils.emailValidation(email)) {
							setErrorText("E-mail isn't valid",
									ErrorField.SIGNUP_EMAIL);
						}

						if (re_email.equals("")) {
							setErrorText("Field is empty",
									ErrorField.SIGNUP_RE_EMAIL);
						}

						if (password.equals("")) {
							setErrorText("Field is empty",
									ErrorField.SIGNUP_PASSWORD);
						} else if (!(password.length() > 3)) {
							setErrorText(
									"Min 4 characters",
									ErrorField.SIGNUP_PASSWORD);
						}
					}
				}	
			}
		});

		return v;
		
	}
	
	@Override
	public void onStart() {
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
		super.onStart();
	};
	
	public enum ErrorField{
		LOGIN_EMAIL,
		LOGIN_PASSWORD,
		SIGNUP_EMAIL,
		SIGNUP_RE_EMAIL,
		SIGNUP_PASSWORD
	}
	
	public static void setErrorText(String errorText, ErrorField field) {
		switch (field) {
		case LOGIN_EMAIL:
			loginEmailError.append(errorText);
			loginEmailError.setVisibility(View.VISIBLE);
			break;
		case LOGIN_PASSWORD:
			loginPasswordError.append(errorText);
			loginPasswordError.setVisibility(View.VISIBLE);
			break;
		case SIGNUP_EMAIL:
			signupEmailError.append(errorText);
			signupEmailError.setVisibility(View.VISIBLE);
			break;
		case SIGNUP_RE_EMAIL:
			signupReEmailError.append(errorText);
			signupReEmailError.setVisibility(View.VISIBLE);
			break;
		case SIGNUP_PASSWORD:
			signupPasswordError.append(errorText);
			signupPasswordError.setVisibility(View.VISIBLE);
			break;
		}
	}
	
	private void clearErrorFields() {
		loginEmailError.setVisibility(View.GONE);
		loginPasswordError.setVisibility(View.GONE);
		signupEmailError.setVisibility(View.INVISIBLE);
		signupReEmailError.setVisibility(View.INVISIBLE);
		signupPasswordError.setVisibility(View.INVISIBLE);
		loginEmailError.setText("");
		loginPasswordError.setText("");
		signupEmailError.setText("");
		signupReEmailError.setText("");
		signupPasswordError.setText("");
	}
}
